package com.imay.live.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imay.base.activity.BaseActivity;
import com.imay.live.activity.authentication.InfoBindActivity;
import com.imay.live.cache.UserInfoCache;
import com.imay.live.model.UserInfoModel;
import com.imay.live.proto.ErrorResult;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.utils.ToastUtil;
import com.imay.live.view.custom.LoadEmptyViewControl;
import com.imay.live.R;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * create by lzx
 * 我的->设置->大V认证
 */
public class VIPAuthenticationActivity extends BaseActivity implements View.OnClickListener {
    LinearLayout src_linear;
    FrameLayout frame_layout;
    UserInfoModel userInfoModel;
    LoadEmptyViewControl loadEmptyViewControl;

    public static void start(Context context) {
        Intent starter = new Intent(context, VIPAuthenticationActivity.class);
//    starter.putExtra();
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_vip_authentication;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setTitle(getString(R.string.vsign_title));
        src_linear = $(R.id.src_linear);
        frame_layout = $(R.id.frame_layout);
        loadEmptyViewControl = new LoadEmptyViewControl(VIPAuthenticationActivity.this);
        loadEmptyViewControl.addLoadView(frame_layout);
        UserInfoModel userInfoModel = UserInfoCache.getUserInfo();
        if (userInfoModel == null || userInfoModel.getUser() == null) {
            loadEmptyViewControl.loadError();
            return;
        }
        if (userInfoModel.getUser().getVip() == 1 && userInfoModel.getUser().getVip() == 2) {
            initViewModel(userInfoModel);
        } else {
            //增加实名认证状态 大V认证状态  (获取自己信息时才有)   stat.liveTime 直播累计时长单位为秒
            ProtoRequestHelper.task(ProtoHttpFactory.getUserInfo(userInfoModel.getUser().getUid())).setTag(this.hashCode()).request(new ProtoCallBack() {
                @Override
                public void onRequestSuccess(String command, String data) {
                    Result<UserInfoModel> result = JSON.parseObject(data, new TypeReference<Result<UserInfoModel>>() {
                    }.getType());
                    if (result.result == CodeSuccess && result.data != null) {
                        initViewModel(result.data);
                    } else {
                        loadEmptyViewControl.loadError();
                    }
                }

                @Override
                public void onRequestError(String command, int code, String message) {
                    loadEmptyViewControl.loadError();
                    ToastUtil.show(VIPAuthenticationActivity.this, ErrorResult.getString(VIPAuthenticationActivity.this, code));
                }
            });
        }


    }

    private void initViewModel(UserInfoModel userInfoModel) {
        if (userInfoModel == null || userInfoModel.getStat() == null) {
            return;
        }
        loadEmptyViewControl.loadcomplete();
        VIPAuthenticationActivity.this.userInfoModel = userInfoModel;
        src_linear.setVisibility(View.VISIBLE);
        TextView tv_peo = $(R.id.tv_peo);
        TextView tv_min = $(R.id.tv_min);
        TextView tv_button = $(R.id.tv_button);

        Drawable ico_05sz_fuhe = getResources().getDrawable(R.mipmap.ico_05sz_fuhe);
        Drawable ico_05sz_bufuhe = getResources().getDrawable(R.mipmap.ico_05sz_bufuhe);
        ico_05sz_bufuhe.setBounds(0, 0, ico_05sz_bufuhe.getMinimumWidth(), ico_05sz_bufuhe.getMinimumHeight());
        ico_05sz_fuhe.setBounds(0, 0, ico_05sz_fuhe.getMinimumWidth(), ico_05sz_fuhe.getMinimumHeight());
        long min = (userInfoModel.getStat().getLiveTime() / 60);
        boolean condition = true;
        if (min < 600) {
            condition = false;
            tv_button.setTag(1);
            tv_min.setCompoundDrawables(null, null, ico_05sz_bufuhe, null);
        } else {
            tv_min.setCompoundDrawables(null, null, ico_05sz_fuhe, null);
        }
        int follower = userInfoModel.getStat().getFollower();
        if (follower < 500) {
            condition = false;
            tv_button.setTag(0);
            tv_peo.setCompoundDrawables(null, null, ico_05sz_bufuhe, null);
        } else {
            tv_peo.setCompoundDrawables(null, null, ico_05sz_fuhe, null);
        }
        if (userInfoModel.getLargeVipStatus() == 0) {
            tv_button.setText(getResources().getString(R.string.vsign_applygo));
            if (condition) {
                tv_button.setOnClickListener(this);
            } else {
                tv_button.setBackground(getResources().getDrawable(R.drawable.shape_corner100_b2));
            }
        } else if (userInfoModel.getLargeVipStatus() == 1) {
            tv_button.setText(getResources().getString(R.string.vsign_applygoing));
            tv_button.setBackground(getResources().getDrawable(R.drawable.shape_corner100_b2));
        } else if (userInfoModel.getLargeVipStatus() == 2) {
            tv_button.setText(getResources().getString(R.string.vsign_applyrego));
            if (condition) {
                tv_button.setOnClickListener(this);
            } else {
                tv_button.setBackground(getResources().getDrawable(R.drawable.shape_corner100_b2));
            }

        } else if (userInfoModel.getLargeVipStatus() == 3) {
            tv_button.setText(getResources().getString(R.string.vsign_applysuccess));
            tv_button.setBackground(getResources().getDrawable(R.drawable.shape_corner100_b2));
        }
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_button) {
            if (v.getTag() == null) {
                InfoBindActivity.startActivityResult(VIPAuthenticationActivity.this, userInfoModel.getRealNameStatus() == 3, userInfoModel.getPhone(), userInfoModel.getUser().getUid());
            } else {
                ToastUtil.show(VIPAuthenticationActivity.this, getResources().getString(R.string.vsign_applyunknow));
            }

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == InfoBindActivity.BIND) {
            if (resultCode == Activity.RESULT_OK) {
                finish();
            }
        }
    }
}
